package com.starsea.book.ui.main.bookshelf.style1.books

import android.content.Context
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.DiffUtil
import androidx.viewbinding.ViewBinding
import com.starsea.book.base.adapter.DiffRecyclerAdapter
import com.starsea.book.ui.newpage.entity.BookShelfEntity

abstract class BaseBooksAdapter<VB : ViewBinding>(context: Context) :
    DiffRecyclerAdapter<BookShelfEntity, VB>(context) {

    var isManage = false

    override val diffItemCallback: DiffUtil.ItemCallback<BookShelfEntity>
        get() = object : DiffUtil.ItemCallback<BookShelfEntity>() {

            override fun areItemsTheSame(
                oldItem: BookShelfEntity,
                newItem: BookShelfEntity
            ): Boolean {
                return false
            }
            override fun areContentsTheSame(
                oldItem: BookShelfEntity,
                newItem: BookShelfEntity
            ): Boolean {
                return false
            }

            override fun getChangePayload(
                oldItem: BookShelfEntity,
                newItem: BookShelfEntity
            ): Any? {
                val bundle = bundleOf()
                if (oldItem.book?.name != newItem.book?.name) {
                    bundle.putString("name", newItem.book?.name)
                }
                if (oldItem.book?.author != newItem.book?.author) {
                    bundle.putString("author", newItem.book?.author)
                }
                if (oldItem.book?.durChapterTitle != newItem.book?.durChapterTitle) {
                    bundle.putString("dur", newItem.book?.durChapterTitle)
                }
                if (oldItem.book?.latestChapterTitle != newItem.book?.latestChapterTitle) {
                    bundle.putString("last", newItem.book?.latestChapterTitle)
                }
                if (oldItem.book?.getDisplayCover() != newItem.book?.getDisplayCover()) {
                    bundle.putString("cover", newItem.book?.getDisplayCover())
                }
                if (oldItem.book?.lastCheckCount != newItem.book?.lastCheckCount
                    || oldItem.book?.durChapterTime != newItem.book?.durChapterTime
                    || oldItem.book?.getUnreadChapterNum() != newItem.book?.getUnreadChapterNum()
                    || oldItem.book?.lastCheckCount != newItem.book?.lastCheckCount
                ) {
                    bundle.putBoolean("refresh", true)
                }
                if (oldItem.book?.latestChapterTime != newItem.book?.latestChapterTime) {
                    bundle.putBoolean("lastUpdateTime", true)
                }
                if (bundle.isEmpty) return null
                return bundle
            }

        }

    fun notification(bookUrl: String) {
        getItems().forEachIndexed { i, it ->
            if (it.book?.bookUrl == bookUrl) {
                notifyItemChanged(i, bundleOf(Pair("refresh", null), Pair("lastUpdateTime", null)))
                return
            }
        }
    }

    fun upLastUpdateTime() {
        notifyItemRangeChanged(0, itemCount, bundleOf(Pair("lastUpdateTime", null)))
    }

    interface CallBack {
        fun open(book: String, position: Int)
        fun openLocal(book: String, position: Int)
        fun openBookInfo(book: String, position: Int)
        fun isUpdate(bookUrl: String): Boolean
    }
}